package com.example.administrator.questionanswer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.administrator.questionanswer.application.MyApplication;
import com.example.administrator.questionanswer.dao.DaoSession;
import com.example.administrator.questionanswer.dao.UserBeanDao;
import com.example.administrator.questionanswer.greendao.UserBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/4/2 0002.
 */

public class RegisterActivity extends Activity {
    @BindView(R.id.edit_user_name)
    EditText editUserName;
    @BindView(R.id.edit_password)
    EditText editPassword;
    @BindView(R.id.btn_login)
    Button btnLogin;
    DaoSession daoSession;
    UserBeanDao userBeanDao;
    @BindView(R.id.img_back)
    ImageView imgBack;

    Handler handler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        MyApplication.getActivies().add(this);
        daoSession = MyApplication.getInstances().getDaoSession();
        userBeanDao = daoSession.getUserBeanDao();
        handler = new Handler();
    }


    @OnClick({R.id.btn_login, R.id.img_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_login:

                final String userName = editUserName.getText().toString();
                final String password = editPassword.getText().toString();
                if (userName == null || "".equals(userName)) {
                    Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (password == null || "".equals(password)) {
                    Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }

                List<UserBean> userBeans = userBeanDao.loadAll();
                if (userBeans.size() > 0) {
                    for (UserBean bean : userBeans) {
                        String name = bean.getName();
                        if (userName.equals(name)) {
                            Toast.makeText(this, "已存在该用户", Toast.LENGTH_SHORT).show();
                            return;
                        } else {
                            UserBean userBean = new UserBean();
                            userBean.setName(userName);
                            userBean.setPassword(password);
                            long insert = userBeanDao.insert(userBean);
                            if (insert > 0) {
                                Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
                                handler.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        Intent intent = new Intent();
                                        intent.putExtra("name", userName);
                                        intent.putExtra("password", password);
                                        setResult(RESULT_OK, intent);
                                        finish();
                                    }
                                }, 1000);

                            }
                        }

                    }
                } else {
                    UserBean userBean = new UserBean();
                    userBean.setName(userName);
                    userBean.setPassword(password);
                    long insert = userBeanDao.insert(userBean);
                    if (insert > 0) {
                        Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                Intent intent = new Intent();
                                intent.putExtra("name", userName);
                                intent.putExtra("password", password);
                                setResult(RESULT_OK, intent);
                                finish();
                            }
                        }, 1000);

                    }
                }


                break;
            case R.id.img_back:
                finish();
                break;
        }
    }
}
